home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / man.el.z / man.el
Encoding:
Text File  |  1998-05-21  |  23.7 KB  |  653 lines

  1. ;;; man.el --- browse UNIX manual pages
  2. ;; Keywords: help
  3.  
  4. ;; Copyright (C) 1985, 1993, 1994, 1996, 1997 Free Software Foundation, Inc.
  5. ;;
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. ;; General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  20. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ;; This file defines "manual-entry", and the remaining definitions all
  23. ;; begin with "Manual-".  This makes the autocompletion on "M-x man" work.
  24. ;; 
  25. ;; Eviscerated 26-Jun-96 by Jamie Zawinski <jwz@netscape.com>.
  26. ;; All that stuff about looking at $MANPATH and building up lists of 
  27. ;; directories was bullshit.  Now we just invoke "man" and format the
  28. ;; output, end of story.
  29. ;;
  30. ;; [ older changelog entries removed, since they're all about code that
  31. ;;   I've deleted. ]
  32.  
  33. (defgroup man nil
  34.   "Browse Unix manual pages"
  35.   :group 'help)
  36.  
  37. (defcustom Manual-program "man" "\
  38. *Name of the program to invoke in order to format the source man pages."
  39.   :type 'string
  40.   :group 'man)
  41.  
  42. (defcustom Manual-buffer-view-mode t "\
  43. *Whether manual buffers should be placed in view-mode.
  44. nil means leave the buffer in fundamental-mode in another window.
  45. t means use `view-buffer' to display the man page in the current window.
  46. Any other value means use `view-buffer-other-window'."
  47.   :type '(radio (const :tag "Fundamental-mode other window" nil)
  48.         (const :tag "View-mode current window" t)
  49.         (sexp :format "%t\n" :tag "View-mode other window" other))
  50.   :group 'man)
  51.  
  52. (defcustom Manual-mode-hook nil
  53.   "Function or functions run on entry to Manual-mode."
  54.   :type 'hook
  55.   :group 'man)
  56.  
  57. (defvar Manual-page-history nil "\
  58. A list of names of previously visited man page buffers.")
  59.  
  60. (defvar Manual-page-minibuffer-history nil "\
  61. Minibuffer completion history for `manual-entry'.")
  62.  
  63. ;; New variables.
  64.  
  65. (defcustom Manual-snip-subchapter
  66.   (not (string-match "solaris" system-configuration))
  67.   "*Should man look in chapter 3 for ctime(3c)?
  68. This is relavent for Solaris and, perhaps, other systems which have 
  69. `man -s 3' not find things in chapter 3c, or other such sub-chapters"
  70.   :type 'boolean
  71.   :group 'man)
  72.  
  73. ;;Here is information on RosettaMan, from Neal.Becker@comsat.com (Neal Becker):
  74.  
  75. ;;RosettaMan is a filter for UNIX manual pages.  It takes as input man
  76. ;;pages formatted for a variety of UNIX flavors (not [tn]roff source)
  77. ;;and produces as output a variety of file formats.  Currently
  78. ;;RosettaMan accepts man pages as formatted by the following flavors of
  79. ;;UNIX: Hewlett-Packard HP-UX, AT&T System V, SunOS, Sun Solaris, OSF/1,
  80. ;;DEC Ultrix, SGI IRIX, Linux, SCO; and produces output for the following
  81. ;;formats: printable ASCII only (stripping page headers and footers),
  82. ;;section and subsection headers only, TkMan, [tn]roff, Ensemble, RTF,
  83. ;;SGML (soon--I finally found a DTD), HTML, MIME, LaTeX, LaTeX 2e, Perl 5's pod.
  84.  
  85. ;;RosettaMan improves on other man page filters in several ways: (1) its
  86. ;;analysis recognizes the structural pieces of man pages, enabling high
  87. ;;quality output, (2) its modular structure permits easy augmentation of
  88. ;;output formats, (3) it accepts man pages formatted with the varient
  89. ;;macros of many different flavors of UNIX, and (4) it doesn't require
  90. ;;modification or cooperation with any other program.
  91.  
  92. ;;RosettaMan is a rewrite of TkMan's man page filter, called bs2tk.  (If
  93. ;;you haven't heard about TkMan, a hypertext man page browser, you
  94. ;;should grab it via anonymous ftp from ftp.cs.berkeley.edu:
  95. ;;/ucb/people/phelps/tkman.tar.Z.)  Whereas bs2tk generated output only for
  96. ;;TkMan, RosettaMan generalizes the process so that the analysis can be
  97. ;;leveraged to new output formats.  A single analysis engine recognizes
  98. ;;section heads, subsection heads, body text, lists, references to other
  99. ;;man pages, boldface, italics, bold italics, special characters (like
  100. ;;bullets), tables (to a degree) and strips out page headers and
  101. ;;footers.  The engine sends signals to the selected output functions so
  102. ;;that an enhancement in the engine improves the quality of output of
  103. ;;all of them.  Output format functions are easy to add, and thus far
  104. ;;average about about 75 lines of C code each.
  105.  
  106.  
  107.  
  108. ;;*** NOTES ON CURRENT VERSION ***
  109.  
  110. ;;Help!  I'm looking for people to help with the following projects.
  111. ;;\(1) Better RTF output format.  The current one works, but could be
  112. ;;made better.  (2) Roff macros that produce text that is easily
  113. ;;parsable.  RosettaMan handles a great variety, but some things, like
  114. ;;H-P's tables, are intractable.  If you write an output format or
  115. ;;otherwise improve RosettaMan, please send in your code so that I may
  116. ;;share the wealth in future releases.
  117.  
  118. ;;This version can try to identify tables (turn this on with the -T
  119. ;;switch) by looking for lines with a large amount of interword spacing,
  120. ;;reasoning that this is space between columns of a table.  This
  121. ;;heuristic doesn't always work and sometimes misidentifies ordinary
  122. ;;text as tables.  In general I think it is impossible to perfectly
  123. ;;identify tables from nroff formatted text.  However, I do think the
  124. ;;heuristics can be tuned, so if you have a collection of manual pages
  125. ;;with unrecognized tables, send me the lot, in formatted form (i.e.,
  126. ;;after formatting with nroff -man), and uuencode them to preserve the
  127. ;;control characters.  Better, if you can think of heuristics that
  128. ;;distinguish tables from ordinary text, I'd like to hear them.
  129.  
  130. ;;Notes for HTML consumers: This filter does real (heuristic)
  131. ;;parsing--no <PRE>!  Man page references are turned into hypertext links.
  132.  
  133. (defcustom Manual-use-rosetta-man (not (null (locate-file "rman" exec-path))) "\
  134. If non-nil, use RosettaMan (rman) to filter man pages.
  135. This makes man-page cleanup virtually instantaneous, instead of
  136. potentially taking a long time."
  137.   :type 'boolean
  138.   :group 'man)
  139.  
  140. (defface man-italic '((t (:italic t)))
  141.   "Manual italics face"
  142.   :group 'man)
  143.  
  144. (defface man-bold '((t (:bold t)))
  145.   "Manual bold face"
  146.   :group 'man)
  147.  
  148. (defface man-heading '((t (:bold t)))
  149.   "Manual headings face"
  150.   :group 'man)
  151.  
  152. (defface man-xref '((t (:underline t)))
  153.   "Manual xrefs face"
  154.   :group 'man)
  155.  
  156.  
  157. (defvar Manual-mode-map
  158.   (let ((m (make-sparse-keymap)))
  159.     (set-keymap-name m 'Manual-mode-map)
  160.     (define-key m "l" 'Manual-last-page)
  161.     (define-key m 'button2 'Manual-follow-xref)
  162.     (define-key m 'button3 'Manual-popup-menu)
  163.     m))
  164.  
  165. ;;;###autoload
  166. (defun manual-entry (topic &optional arg silent)
  167.   "Display the Unix manual entry (or entries) for TOPIC."
  168.   (interactive
  169.    (list (let* ((fmh "-A-Za-z0-9_.:")
  170.         (default (save-excursion
  171.                (buffer-substring
  172.                 (progn
  173.                   (re-search-backward "\\sw" nil t)
  174.                   (skip-chars-backward fmh) (point))
  175.                 (progn (skip-chars-forward fmh) (point)))))
  176.         (thing (read-string
  177.             (if (equal default "") "Manual entry: "
  178.               (concat "Manual entry: (default " default ") "))
  179.             nil 'Manual-page-minibuffer-history)))
  180.        (if (equal thing "") default thing))
  181.      (prefix-numeric-value current-prefix-arg)))
  182.   (or arg (setq arg 1))
  183.   (let (section apropos-mode)
  184.     (let ((case-fold-search nil))
  185.       (if (and (null section)
  186.            (string-match "\\`[ \t]*\\([^( \t]+\\)[ \t]*(\\(.+\\))[ \t]*\\'"
  187.                  topic))
  188.       (setq section (match-string 2 topic)
  189.         topic (match-string 1 topic))
  190.     (if (string-match "\\`[ \t]*-k[ \t]+\\([^ \t]+\\)\\'" topic)
  191.         (setq section "-k"
  192.           topic (substring topic (match-beginning 1))))))
  193.  
  194.     (when Manual-snip-subchapter
  195.       ;; jwz: turn section "3x11" and "3n" into "3".
  196.       (if (and section (string-match "\\`\\([0-9]+\\)[^0-9]" section))
  197.       (setq section (match-string 1 section))))
  198.  
  199.     (if (equal section "-k")
  200.     (setq apropos-mode t))
  201.  
  202.     (let ((bufname (concat "Man"
  203.                (when apropos-mode " apropos")
  204.                ": " topic
  205.                (when section (concat "(" section ")"))))
  206.       (temp-buffer-show-function 
  207.        (cond ((eq 't Manual-buffer-view-mode)
  208.           'view-buffer)
  209.          ((eq 'nil Manual-buffer-view-mode)
  210.           temp-buffer-show-function)
  211.          (t
  212.           'view-buffer-other-window))))
  213.  
  214.       (cond ((get-buffer bufname)
  215.          ;; reselect an old man page buffer if it exists already.
  216.          (save-excursion
  217.            (set-buffer (get-buffer bufname))
  218.            (Manual-mode))
  219.          (if temp-buffer-show-function
  220.          (funcall temp-buffer-show-function (get-buffer bufname))
  221.            (display-buffer bufname)))
  222.         (t
  223.          (with-output-to-temp-buffer bufname
  224.            (buffer-disable-undo standard-output)
  225.            (save-excursion
  226.          (set-buffer standard-output)
  227.          (setq buffer-read-only nil)
  228.          (erase-buffer)
  229.  
  230.          (let ((args (list topic))
  231.                args-string)
  232.            (if section
  233.                (setq args
  234.                  (if (and (eq system-type 'usg-unix-v)
  235.                       (null apropos-mode))
  236.                  (cons "-s" (cons section args))
  237.                    (cons section args))))
  238.            (setq args-string
  239.              (mapconcat 'identity (cons Manual-program args) " "))
  240.            (if (string-match "\\`\\([^ \t/]*/\\)+" args-string)
  241.                (setq args-string
  242.                  (substring args-string (match-end 0))))
  243.  
  244.            (message "%s (running...)" args-string)
  245.            (apply 'call-process Manual-program nil '(t nil) nil args)
  246.  
  247.            (if (< (buffer-size) 200)
  248.                (progn
  249.              (kill-buffer (current-buffer))
  250.              (error "%s not found" args-string)))
  251.  
  252.            (message "%s (cleaning...)" args-string)
  253.            (Manual-nuke-nroff-bs apropos-mode)
  254.            (message "%s (done.)" args-string))
  255.          (set-buffer-modified-p nil)
  256.          (Manual-mode)))))
  257.       (let ((page (if section
  258.               (concat topic "(" section ")")
  259.             topic)))
  260.     (setq Manual-page-history
  261.           (cons (buffer-name)
  262.             (delete (buffer-name) Manual-page-history))
  263.           Manual-page-minibuffer-history
  264.           (cons page (delete page Manual-page-minibuffer-history))))))
  265.  
  266.   (message nil)
  267.   t)
  268.  
  269. (defun Manual-mode ()
  270.   (kill-all-local-variables)
  271.   (setq buffer-read-only t)
  272.   (use-local-map Manual-mode-map)
  273.   (setq major-mode 'Manual-mode
  274.     mode-name "Manual")
  275.   ;; man pages with long lines are buggy!
  276.   ;; This looks slightly better if they only
  277.   ;; overran by a couple of chars.
  278.   (setq truncate-lines t)
  279.   ;; turn off horizontal scrollbars in this buffer
  280.   (when (featurep 'scrollbar)
  281.     (set-specifier scrollbar-height (cons (current-buffer) 0)))
  282.   (make-local-hook 'kill-buffer-hook)
  283.   (add-hook 'kill-buffer-hook #'(lambda ()
  284.                   (setq Manual-page-history
  285.                     (delete (buffer-name)
  286.                         Manual-page-history)))
  287.         nil t)
  288.   (run-hooks 'Manual-mode-hook))
  289.  
  290. (defun Manual-last-page ()
  291.   (interactive)
  292.   (if Manual-page-history
  293.       (let ((page (pop Manual-page-history)))
  294.     (if page
  295.         (progn
  296.           (get-buffer page)
  297.           (cons Manual-page-history page)
  298.           (switch-to-buffer page))))
  299.     (error "No manual page buffers found. Use `M-x manual-entry'")))
  300.  
  301.  
  302. (defmacro Manual-delete-char (n)
  303.   ;; in v19, delete-char is compiled as a function call, but delete-region
  304.   ;; is byte-coded, so it's much faster.  (We were spending 40% of our time
  305.   ;; in delete-char alone.)
  306.   (list 'delete-region '(point) (list '+ '(point) n)))
  307.  
  308. ;; Hint: BS stands for more things than "back space"
  309. (defun Manual-nuke-nroff-bs (&optional apropos-mode)
  310.   (interactive "*")
  311.   (if Manual-use-rosetta-man
  312.       (call-process-region (point-min) (point-max) "rman" t t nil)
  313.     ;;
  314.     ;; turn underlining into italics
  315.     ;;
  316.     (goto-char (point-min))
  317.     (while (search-forward "_\b" nil t)
  318.       ;; searching for underscore-backspace and then comparing the
  319.       ;; following chars until the sequence ends turns out to be much
  320.       ;; faster than searching for a regexp which matches the whole
  321.       ;; sequence.
  322.       (let ((s (match-beginning 0)))
  323.     (goto-char s)
  324.     (while (and (= (following-char) ?_)
  325.             (= (char-after (1+ (point))) ?\b))
  326.       (Manual-delete-char 2)
  327.       (forward-char 1))
  328.     (set-extent-face (make-extent s (point)) 'man-italic)))
  329.     ;;
  330.     ;; turn overstriking into bold
  331.     ;;
  332.     (goto-char (point-min))
  333.     (while (re-search-forward "\\([^\n]\\)\\(\b\\1\\)" nil t)
  334.       ;; Surprisingly, searching for the above regexp is faster than searching
  335.       ;; for a backspace and then comparing the preceding and following chars,
  336.       ;; I presume because there are many false matches, meaning more funcalls
  337.       ;; to re-search-forward.
  338.       (let ((s (match-beginning 0)))
  339.     (goto-char s)
  340.     ;; Some systems (SGI) overstrike multiple times, eg, "M\bM\bM\bM".
  341.     (while (looking-at "\\([^\n]\\)\\(\b\\1\\)+")
  342.       (delete-region (+ (point) 1) (match-end 0))
  343.       (forward-char 1))
  344.     (set-extent-face (make-extent s (point)) 'man-bold)))
  345.     ;;
  346.     ;; hack bullets: o^H+ --> +
  347.     (goto-char (point-min))
  348.     (while (search-forward "\b" nil t)
  349.       (Manual-delete-char -2))
  350.  
  351.     (if (> (buffer-size) 100) ; minor kludge
  352.     (Manual-nuke-nroff-bs-footers))
  353.     ) ;; not Manual-use-rosetta-man
  354.   ;;
  355.   ;; turn subsection header lines into bold
  356.   ;;
  357.   (goto-char (point-min))
  358.   (if apropos-mode
  359.       (while (re-search-forward "[a-zA-Z0-9] ([0-9]" nil t)
  360.     (forward-char -2)
  361.     (delete-backward-char 1))
  362.  
  363.     ;;    (while (re-search-forward "^[^ \t\n]" nil t)
  364.     ;;      (set-extent-face (make-extent (match-beginning 0)
  365.     ;;                                   (progn (end-of-line) (point)))
  366.     ;;                      'man-heading))
  367.  
  368.     ;; boldface the first line
  369.     (if (looking-at "[^ \t\n].*$")
  370.     (set-extent-face (make-extent (match-beginning 0) (match-end 0))
  371.              'man-bold))
  372.  
  373.     ;; boldface subsequent title lines
  374.     ;; Regexp to match section headers changed to match a non-indented
  375.     ;; line preceded by a blank line and followed by an indented line. 
  376.     ;; This seems to work ok for manual pages but gives better results
  377.     ;; with other nroff'd files
  378.     (while (re-search-forward "\n\n\\([^ \t\n].*\\)\n[ \t]+[^ \t\n]" nil t)
  379.       (goto-char (match-end 1))
  380.       (set-extent-face (make-extent (match-beginning 1) (match-end 1))
  381.                'man-heading)
  382.       (forward-line 1))
  383.     )
  384.  
  385.   (if Manual-use-rosetta-man
  386.       nil
  387.     ;; Zap ESC7,  ESC8, and ESC9
  388.     ;; This is for Sun man pages like "man 1 csh"
  389.     (goto-char (point-min))
  390.     (while (re-search-forward "\e[789]" nil t)
  391.       (replace-match "")))
  392.   
  393.   ;; Nuke blanks lines at start.
  394.   ;;  (goto-char (point-min))
  395.   ;;  (skip-chars-forward "\n")
  396.   ;;  (delete-region (point-min) (point))
  397.  
  398.   (Manual-mouseify-xrefs)
  399.   )
  400.  
  401. (fset 'nuke-nroff-bs 'Manual-nuke-nroff-bs) ; use old name
  402.  
  403.  
  404. (defun Manual-nuke-nroff-bs-footers ()
  405.   "For info see comments in packages/man.el"
  406.   ;; Nuke headers and footers.
  407.   ;;
  408.   ;; nroff assumes pages are 66 lines high.  We assume that, and that the
  409.   ;; first and last line on each page is expendible.  There is no way to
  410.   ;; tell the difference between a page break in the middle of a paragraph
  411.   ;; and a page break between paragraphs (the amount of extra whitespace
  412.   ;; that nroff inserts is the same in both cases) so this might strip out
  413.   ;; a blank line were one should remain.  I think that's better than
  414.   ;; leaving in a blank line where there shouldn't be one.  (Need I say
  415.   ;; it: FMH.)
  416.   ;;
  417.   ;; Note that if nroff spits out error messages, pages will be more than
  418.   ;; 66 lines high, and we'll lose badly.  That's ok because standard
  419.   ;; nroff doesn't do any diagnostics, and the "gnroff" wrapper for groff
  420.   ;; turns off error messages for compatibility.  (At least, it's supposed
  421.   ;; to.)
  422.   ;; 
  423.   (goto-char (point-min))
  424.   ;; first lose the status output
  425.   (let ((case-fold-search t))
  426.     (if (and (not (looking-at "[^\n]*warning"))
  427.          (looking-at "Reformatting.*\n"))
  428.     (delete-region (match-beginning 0) (match-end 0))))
  429.  
  430.   ;; kludge around a groff bug where it won't keep quiet about some
  431.   ;; warnings even with -Wall or -Ww.
  432.   (cond ((looking-at "grotty:")
  433.      (while (looking-at "grotty:")
  434.        (delete-region (point) (progn (forward-line 1) (point))))
  435.      (if (looking-at " *done\n")
  436.          (delete-region (point) (match-end 0)))))
  437.  
  438.   (let ((pages '())
  439.     p)
  440.     ;; collect the page boundary markers before we start deleting, to make
  441.     ;; it easier to strip things out without changing the page sizes.
  442.     (while (not (eobp))
  443.       (forward-line 66)
  444.       (setq pages (cons (point-marker) pages)))
  445.     (setq pages (nreverse pages))
  446.     (while pages
  447.       (goto-char (car pages))
  448.       (set-marker (car pages) nil)
  449.       ;;
  450.       ;; The lines are: 3 blank; footer; 6 blank; header; 3 blank.
  451.       ;; We're in between the previous footer and the following header,
  452.       ;;
  453.       ;; First lose 3 blank lines, the header, and then 3 more.
  454.       ;;
  455.       (setq p (point))
  456.       (skip-chars-forward "\n")
  457.       (delete-region p (point))
  458.       (and (looking-at "[^\n]+\n\n?\n?\n?")
  459.        (delete-region (match-beginning 0) (match-end 0)))
  460.       ;;
  461.       ;; Next lose the footer, and the 3 blank lines after, and before it.
  462.       ;; But don't lose the last footer of the manual entry; that contains
  463.       ;; the "last change" date, so it's not completely uninteresting.
  464.       ;; (Actually lose all blank lines before it; sh(1) needs this.)
  465.       ;;
  466.       (skip-chars-backward "\n")
  467.       (beginning-of-line)
  468.       (if (null (cdr pages))
  469.       nil
  470.     (and (looking-at "[^\n]+\n\n?\n?\n?")
  471.          (delete-region (match-beginning 0) (match-end 0))))
  472.       (setq p (point))
  473.       (skip-chars-backward "\n")
  474.       (if (> (- p (point)) 4)
  475.       (delete-region (+ 2 (point)) p)
  476.     (delete-region (1+ (point)) p))
  477. ;      (and (looking-at "\n\n?\n?")
  478. ;       (delete-region (match-beginning 0) (match-end 0)))
  479.  
  480.       (setq pages (cdr pages)))
  481.     ;;
  482.     ;; Now nuke the extra blank lines at the beginning and end.
  483.     (goto-char (point-min))
  484.     (if (looking-at "\n+")
  485.     (delete-region (match-beginning 0) (match-end 0)))
  486.     (forward-line 1)
  487.     (if (looking-at "\n\n+")
  488.     (delete-region (1+ (match-beginning 0)) (match-end 0)))
  489.     (goto-char (point-max))
  490.     (skip-chars-backward "\n")
  491.     (delete-region (point) (point-max))
  492.     (beginning-of-line)
  493.     (forward-char -1)
  494.     (setq p (point))
  495.     (skip-chars-backward "\n")
  496.     (if (= ?\n (following-char)) (forward-char 1))
  497.     (if (> (point) (1+ p))
  498.     (delete-region (point) p))
  499.     ))
  500.  
  501. (defun Manual-mouseify-xrefs ()
  502.   (goto-char (point-min))
  503.   ;; skip the top line of manual pages, but not apropos listings.
  504.   (unless apropos-mode (forward-line 1))
  505.   (let ((case-fold-search nil)
  506.     s e name splitp extent)
  507.     ;; possibly it would be faster to rewrite this expression to search for
  508.     ;; a less common sequence first (like "([0-9]") and then back up to see
  509.     ;; if it's really a match.  This function is 15% of the total time, 13%
  510.     ;; of which is this call to re-search-forward.
  511.     (while (re-search-forward "[a-zA-Z_][-a-zA-Z0-9_.:]*([0-9][a-zA-Z0-9]*)"
  512.                   nil t)
  513.       (setq s (match-beginning 0)
  514.         e (match-end 0)
  515.         name (buffer-substring s e)
  516.         splitp nil)
  517.  
  518.       (goto-char s)
  519.       ;; if this is a hyphenated xref, we're on the second line, 1st char now.
  520.       (when (progn
  521.           (beginning-of-line)
  522.           (and (looking-at (concat "^[ \t]+" (regexp-quote name)))
  523.            (progn
  524.              (backward-char 1)
  525.              (or (equal (char-before) ?-)
  526.              (equal (char-before) ?\255)))
  527.            (setq s (progn
  528.                  (skip-chars-backward "-\255_a-zA-Z0-9")
  529.                  (point))
  530.              name (buffer-substring s e))))
  531.     (setq splitp t)
  532.     ;; delete the spaces and dash from `name'
  533.     (let (i)
  534.       (while (setq i (string-match "[-\255 \n\t]+" name i))
  535.         (setq name (concat (substring name 0 i)
  536.                    (substring name (match-end 0)))
  537.           i (1+ i)))))
  538.  
  539.       ;; if there are upper case letters in the section, downcase them.
  540.       (if (string-match "(.*[A-Z]+.*)$" name)
  541.       (setq name (concat (substring name 0 (match-beginning 0))
  542.                  (downcase (substring name (match-beginning 0))))))
  543.  
  544.       ;; if the xref was hyphenated, don't highlight the indention spaces.
  545.       (if splitp
  546.       (progn
  547.         (setq extent (make-extent s (progn (goto-char s) (end-of-line) (point))))
  548.         (set-extent-property extent 'man (list 'Manual-follow-xref name))
  549.         (set-extent-property extent 'highlight t)
  550.         (set-extent-face extent 'man-xref)
  551.         (goto-char e)
  552.         (skip-chars-backward "-_a-zA-Z0-9()")
  553.         (setq extent (make-extent (point) e)))
  554.     (setq extent (make-extent s e)))
  555.       (set-extent-property extent 'man (list 'Manual-follow-xref name))
  556.       (set-extent-property extent 'highlight t)
  557.       (set-extent-face extent 'man-xref)
  558.       (goto-char e))))
  559.  
  560. (defun Manual-follow-xref (&optional name-or-event)
  561.   "Invoke `manual-entry' on the cross-reference under the mouse.
  562. When invoked noninteractively, the arg may be an xref string to parse instead."
  563.   (interactive "e")
  564.   (if (eventp name-or-event)
  565.       (let* ((p (event-point name-or-event))
  566.          (extent (and p (extent-at p
  567.                  (event-buffer name-or-event)
  568.                  'highlight)))
  569.          (data (and extent (extent-property extent 'man))))
  570.     (if (eq (car-safe data) 'Manual-follow-xref)
  571.         (eval data)
  572.       (error "no manual cross-reference there.")))
  573.     (or (manual-entry name-or-event)
  574.     ;; If that didn't work, maybe it's in a different section than the
  575.     ;; man page writer expected.  For example, man pages tend assume
  576.     ;; that all user programs are in section 1, but X tends to generate
  577.     ;; makefiles that put things in section "n" instead...
  578.     (and (string-match "[ \t]*([^)]+)\\'" name-or-event)
  579.          (progn
  580.            (message "No entries found for %s; checking other sections..."
  581.             name-or-event)
  582.            (manual-entry
  583.         (substring name-or-event 0 (match-beginning 0))
  584.         nil t))))))
  585.  
  586. (defun Manual-popup-menu (&optional event)
  587.   "Pops up a menu of cross-references in this manual page.
  588. If there is a cross-reference under the mouse button which invoked this
  589. command, it will be the first item on the menu.  Otherwise, they are
  590. on the menu in the order in which they appear in the buffer."
  591.   (interactive "e")
  592.   (let ((buffer (current-buffer))
  593.     (sep "---")
  594.     xref items)
  595.     (cond (event
  596.        (setq buffer (event-buffer event))
  597.        (let* ((p (event-point event))
  598.           (extent (and p (extent-at p buffer 'highlight)))
  599.           (data (and extent (extent-property extent 'man))))
  600.          (if (eq (car-safe data) 'Manual-follow-xref)
  601.          (setq xref (nth 1 data))))))
  602.     (if xref (setq items (list sep xref)))
  603.     (map-extents #'(lambda (extent ignore)
  604.              (let ((data (extent-property extent 'man)))
  605.                (if (and (eq (car-safe data) 'Manual-follow-xref)
  606.                 (not (member (nth 1 data) items)))
  607.                (setq items (cons (nth 1 data) items)))
  608.             nil))
  609.          buffer)
  610.     (if (eq sep (car items)) (setq items (cdr items)))
  611.     (let ((popup-menu-titles t))
  612.       (and (null items) (setq popup-menu-titles nil))
  613.       (popup-menu
  614.        (cons "Manual Entry"
  615.          (mapcar #'(lambda (item)
  616.              (if (eq item sep)
  617.                  item
  618.                            (vector item
  619.                                    (list 'Manual-follow-xref item) t)))
  620.              (nreverse items)))))))
  621.  
  622. (defun pager-cleanup-hook ()
  623.   "cleanup man page if called via $PAGER"
  624.   (let ((buf-name (or buffer-file-name (buffer-name))))
  625.     (if (or (string-match "^/tmp/man[0-9]+" buf-name)
  626.         (string-match ".*/man/\\(man\\|cat\\)[1-9a-z]/" buf-name))
  627.         (let (buffer manpage)
  628.           (require 'man)
  629.           (goto-char (point-min))
  630.           (setq buffer-read-only nil)
  631.           (Manual-nuke-nroff-bs)
  632.           (goto-char (point-min))
  633.           (if (re-search-forward "[^ \t]")
  634.           (goto-char (- (point) 1)))
  635.           (if (looking-at "\\([a-zA-Z0-9]+\\)[ \t]*(")
  636.           (setq manpage (buffer-substring (match-beginning 1)
  637.                           (match-end 1)))
  638.         (setq manpage "???"))
  639.           (setq buffer
  640.             (rename-buffer (generate-new-buffer-name
  641.                     (concat "Man: " manpage)))
  642.             buffer-file-name nil)
  643.           (goto-char (point-min))
  644.           (insert (format "%s\n" buf-name))
  645.           (goto-char (point-min))
  646.           (buffer-disable-undo buffer)
  647.           (set-buffer-modified-p nil)
  648.           (Manual-mode)
  649.           ))))
  650.  
  651. (add-hook 'server-visit-hook 'pager-cleanup-hook)
  652. (provide 'man)
  653.